1349B - Orac and Medians - CodeForces Solution


constructive algorithms greedy math *2000

Please click on ads to support us..

Python Code:

from sys import stdin
input = stdin.readline 

for case in range(int(input())):
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    if n == 1:
        if a[0] == k:
            print("yes")
        else:
            print("no")
        continue

    k_inds = [ind for ind, e in enumerate(a) if e == k]

    a = [1 if e >= k else -1 for e in a]
    max_including_after = [float('-inf')] * n; max_after = [float('-inf')] * n
    for i in range(n - 2, -1, -1):
        max_including_after[i] = max(a[i] + a[i + 1], a[i] + max_including_after[i + 1])
        max_after[i] = max(max_including_after[i], max_after[i + 1])

    max_including_before = [float('-inf')] * n; max_before = [float('-inf')] * n
    for i in range(1, n):
        max_including_before[i] = max(a[i] + a[i - 1], a[i] + max_including_before[i - 1])
        max_before[i] = max(max_including_before[i], max_before[i - 1])

    ans = False
    for i in k_inds:
        if max_after[i] > 0 or max_before[i] > 0:
            ans = True 

    print("yes") if ans else print("no")




    

C++ Code:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                  Solution
//                    Webbly, 27.01.2023
//
//
//                    Arsen ne katai
//
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////



//#include<bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <set>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("fast-loops")
/**
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC target("avx")
*/
#define ll long long
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define flush cout.flush()

using namespace std;

const ll mod = (ll)1e9 + 7, mod3 = 998244353, inf = (ll)2e15, P = 31;

ll binpow (ll a, ll b){
    ll ans = 1;
    while(b){
        if (b & 1){
            ans *= a;

            ans %= mod;
        }

        b >>= 1;
        a *= a;

        a %= mod;
    }
    return ans;
}
ll gcd(ll a, ll b){
    return (b ? gcd(b, a % b) : a);
}
ll nums(ll g){
    ll cur = 0;
    while(g){
        cur++, g /= 10;
    }

    return cur;
}
ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

vector <ll> fac(ll n){
    ll i = 2;

    vector <ll> ans;

    ll save = n;

    while(i * i <= save){
        while(n % i == 0){
            ans.pb(i);

            n /= i;
        }

        i++;
    }

    if (n > 1) ans.pb(n);

    return ans;
}

ll get1(ll q, ll g){
    ll cur = 1;

    for (ll i = q; i > g; i--){
        cur *= i;
    }

    return cur;
}
struct T{
    ll l, r, id;
};

struct tr{
    ll l, r;
};

ll n, m, k, a[1000005], b[1000005];

string pre;

ll check(ll x){
    ll ans = 0;

    for (ll i = 1; i <= n; i++){
        ans += (x - k) * (ll)(a[i] / x);
    }

    return ans;
}

void query(){
    cin >> n >> k;

    bool check = 0;

    for (ll i = 1; i <= n; i++){
        cin >> a[i];

        if (a[i] == k) check = 1;
    }

    if (!check){
        cout << "no\n";

        return;
    }

    if (n == 1){
        cout << "yes\n";

        return;
    }

    for (ll i = 2; i <= n; i++){
        if (min(a[i], a[i - 1]) >= k){
            cout << "yes\n";

            return;
        }

        if (i < n){
            b[1] = a[i - 1], b[2] = a[i], b[3] = a[i + 1];

            sort (b + 1, b + 4);

            if (b[2] >= k){
                cout << "yes\n";

                return;
            }
        }
    }

    cout << "no\n";
}

int main(){
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    //freopen("justforfood.in", "r", stdin);
    //freopen("justforfood.out", "w", stdout);

    ll TT = 1, tests = 1;

    cin >> TT;

    while(TT--){
        //cout << "Case " << tests << ": ";

        query();

        //tests++;
    }

    return 0;
}
/**


2 3 1

3 1 2

2


*/


Comments

Submit
0 Comments
More Questions

1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms